package com.dwh.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dwh.exception.UserException;
import com.dwh.model.User;
import com.dwh.msg.Result;
import com.dwh.msg.ResultEnum;
import com.dwh.repository.UserRepository;
import com.dwh.service.UserService;
import com.dwh.util.ResultUtil;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource
	private UserRepository userRePository;

	@Override
	public List<User> findAllUser() {
		return userRePository.findAll();
	}

	@Override
	public User addUser(User user) {
		return userRePository.save(user);
	}

	@Override
	public User findUserById(int id) {
		return userRePository.findOne(id);
	}

	@Override
	public List<User> findUserByName(String name) {
		return userRePository.findByName(name);
	}

	@Override
	public User modifyUser(User user) {
		return userRePository.save(user);
	}

	@Override
	public void delUser(int id) {
		userRePository.delete(id);
	}

	@Override
	public Result<User> addOneUser(User user) throws Exception {
		if (user.getAge() < 10) {
//			throw new RuntimeException("你可能还在上小学");//无法传递code
//			throw new UserException(100, "你可能还在上小学");
			throw new UserException(ResultEnum.PRIMARY_ERR);
		} else if (user.getAge() > 10 && user.getAge() < 16) {
//			throw new RuntimeException("你可能还在上初中");
//			throw new UserException(101, "你可能还在上初中");
			throw new UserException(ResultEnum.MIDDLE_ERR);
		}
		/*3、
		 * if (user.getAge() < 10) {
			return ResultUtil.error(100, "你可能还在上小学");//返回码和消息乱，不易统一维护
		} else if (user.getAge() > 10 && user.getAge() < 16) {
			return ResultUtil.error(101, "你可能还在上初中");
		}*/
		/*
		 * 2、
		 * if (user.getAge() < 10) {
			Result<User> result = new Result<>();
			result.setCode(100);
			result.setMsg("你可能还在上小学");
			return result;
		} else if (user.getAge() > 10 && user.getAge() < 16) {
			Result<User> result = new Result<>();
			result.setCode(101);
			result.setMsg("你可能还在上初中");
			return result;
		}*/
		/*
		 * 1、
		 * if (user.getAge() < 10) {
			return 1;
			//你可能还在上小学
		} else if (user.getAge() > 10 && user.getAge() < 16) {
			//你可能还在上初中
			return 2;
		}*/
		return ResultUtil.success(user);
		
	}

}
